package com.itheima.web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.MenuSet;
import com.itheima.service.MenuService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @description:
 * @author: dalao
 * @date: 2020/4/17-11:01
 * @Version: 1.0.0
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;

    @RequestMapping("/getAllMenu")
    public Result getAllMenu(){
       List<Menu> menus =  menuService.getAllMenu();
       return Result.OK("" , menus);
    }
    @RequestMapping("/getAllMenuSet")
    @PreAuthorize("hasAuthority('MENU_QUERY')")
    public Result getAllMenuSet(){
        List<MenuSet> menus =  menuService.getAllMenuSet();
        return Result.OK("" , menus);
    }

    @RequestMapping("/addMenu")
    @PreAuthorize("hasAuthority('MENU_ADD')")
    public Result addMenu(@RequestBody Menu menu){
        if(null != menu){
            menuService.addMenu(menu);
            return Result.OK("添加成功");
        }
        return Result.ERROR("添加失败");
    }

    @RequestMapping("/deleteMenu")
    @PreAuthorize("hasAuthority('MENU_DELETE')")
    public Result deleteMenu(Integer menuId){
        menuService.deleteMenu(menuId);
        return Result.OK("删除成功");
    }
    @RequestMapping("/addOneLevelMenu")
    @PreAuthorize("hasAuthority('MENU_ADD')")
    public Result addOneLevelMenu(@RequestBody Menu menu){
        if(null != menu){
            menuService.addOneLevelMenu(menu);
            return Result.OK("添加成功");
        }
        return Result.ERROR("添加失败");
    }
}
